#include "showwidget.h"
#include"modifydialog.h"
#include "ui_showwidget.h"
ShowWidget::ShowWidget(QWidget *parent,QString name) :
    QWidget(parent),userName(name),
    ui(new Ui::ShowWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("车库管理系统");
    this->SetConnect();
    ui->radioButton->setVisible(false);
    ui->radioButton_2->setVisible(false);
    ui->label_2->setText(QString("用户：%1").arg(userName));

}

ShowWidget::~ShowWidget()
{
    delete ui;
}
void ShowWidget::Select(const std::string& s)
{
    this->query.exec(QString(s.c_str()));
    if(!this->query.isActive())
    {

        QMessageBox::warning(this, tr("ERROR"),
                             tr("没有权限访问 "
                                ""),
                             QMessageBox::Yes
                             | QMessageBox::Cancel);
    }
    int count = 0;
    model->removeRows(0,model->rowCount());//对于查询操作，要先将表中的数据进行清空，而不将表头数据进行删除
    int row = query.record().count();
    while(this->query.next())
    {
        for(int i=0;i<row;++i)
        {
            this->model->setItem(count,i,new QStandardItem(query.value(i).toString()));
        }
        ++count;
    }
}

void ShowWidget::Insert()
{

    if(userName=="root")
    {
        InsertDialog* insert = new InsertDialog(this->windowTitle(), this);
        insert->exec();
    }
    else
    {//队友普通用户不允许对用户表以及车位表的插入
        qDebug()<<this->windowTitle();
        if(this->windowTitle()=="用户表" || this->windowTitle()=="车位表")
        {
            QMessageBox::warning(this,"权限不足","没有权限进行插入",QMessageBox::Ok);

        }
        else{
            InsertDialog* insert = new InsertDialog(this->windowTitle(), this);
            insert->exec();
        }
    }
}
void ShowWidget::Update()
{
    if(userName=="root")
    {
        ModifyDialog *d = new ModifyDialog();
        d->SetWindowTitle(this->windowTitle());
        d->SetQuery(this->query);
        d->AddItem();
        d->exec();
    }
    else
    {
        QMessageBox::warning(this,"权限不足","没有权限进行更新",QMessageBox::Ok);
    }
}
void ShowWidget::Delete()
{
    if(userName=="root")
    {
        DeleteDialog *p = new DeleteDialog(this->windowTitle(),query,this,false);
        p->InsertComBox();
        p->SetConnect();
        p->exec();
    }
    else
    {
        QMessageBox::warning(this,"权限不足","没有权限进行删除",QMessageBox::Ok);
    }
}
void ShowWidget::SetCar_setModel()
{
    ui->radioButton->setVisible(false);
    ui->radioButton_2->setVisible(false);
    Flush_RadioButton();
    if(this->windowTitle()!="root")
    {
        ui->Insert_Button->setVisible(false);
        ui->Delete_Button->setVisible(false);
        ui->Edit_Button->setVisible(false);
    }
    this->model = new QStandardItemModel(ui->tableView);
    this->model->setColumnCount(5);
    this->model->setHeaderData(0,Qt::Horizontal, "车位号");
    this->model->setHeaderData(1, Qt::Horizontal, "是否有车");
    this->model->setHeaderData(2,Qt::Horizontal, "离出口距离(m)");
    this->model->setHeaderData(3,Qt::Horizontal, "价格(小时/元)");
    this->model->setHeaderData(4,Qt::Horizontal, "超时价格(小时/元)");


    Select("select * from 车位");

   ui->radioButton->setText("车位号");
   ui->radioButton_2->setText("是否有车");
   ui->radioButton->setVisible(true);
   ui->radioButton_2->setVisible(true);


    ui->tableView->setModel(this->model);
}
void ShowWidget::SetOutModel()
{
    ui->radioButton->setVisible(false);
    ui->radioButton_2->setVisible(false);
    if(userName!="root")
    {
        ui->Insert_Button->setVisible(true);
        ui->Insert_Button->setText("开出");
        ui->Delete_Button->setVisible(false);
        ui->Edit_Button->setVisible(false);
    }
    Flush_RadioButton();
    this->model = new QStandardItemModel(ui->tableView);
    this->model->setColumnCount(5);
    this->model->setHeaderData(0, Qt::Horizontal, "开出ID");
    this->model->setHeaderData(1, Qt::Horizontal, "车位ID");
    this->model->setHeaderData(2, Qt::Horizontal, "开出人员");
    this->model->setHeaderData(3, Qt::Horizontal, "开出时间");
    this->model->setHeaderData(4, Qt::Horizontal, "车牌");
    Select("select * from 开出");

    ui->tableView->setModel(this->model);
    ui->radioButton->setText("开出人员");
    ui->radioButton_2->setText("车牌");
    ui->radioButton->setVisible(true);
    ui->radioButton_2->setVisible(true);
}
void ShowWidget::SetUserModel()
{
    ui->radioButton->setVisible(false);
    ui->radioButton_2->setVisible(false);
    Flush_RadioButton();
    if(this->windowTitle()!="root")
    {
        ui->Insert_Button->setVisible(false);
        ui->Delete_Button->setVisible(false);
        ui->Edit_Button->setVisible(false);
    }
    this->model = new QStandardItemModel(ui->tableView);
    this->model->setColumnCount(4);
    this->model->setHeaderData(0,Qt::Horizontal, "用户ID");
    this->model->setHeaderData(1,Qt::Horizontal, "用户名");
    this->model->setHeaderData(2,Qt::Horizontal, "电话");
    this->model->setHeaderData(3,Qt::Horizontal, "车牌");
    Select("select * from 用户");


    ui->tableView->setModel(this->model);
    ui->radioButton->setText("用户名");
    ui->radioButton_2->setText("车牌");
    ui->radioButton->setVisible(true);
    ui->radioButton_2->setVisible(true);

}
void ShowWidget::SetPreModel()
{
    ui->radioButton->setVisible(false);
    ui->radioButton_2->setVisible(false);
    Flush_RadioButton();
    if(userName!="root")
    {
        ui->Insert_Button->setVisible(true);
        ui->Insert_Button->setText("预定");
        ui->Delete_Button->setVisible(false);
        ui->Edit_Button->setVisible(false);
    }
    this->model = new QStandardItemModel(ui->tableView);
    this->model->setColumnCount(5);
    this->model->setHeaderData(0,Qt::Horizontal, "预定ID");
    this->model->setHeaderData(1,Qt::Horizontal, "预定开出时间");
    this->model->setHeaderData(2,Qt::Horizontal, "预定开入时间");
    this->model->setHeaderData(3,Qt::Horizontal, "预留电话");
    this->model->setHeaderData(4, Qt::Horizontal, "预定车位");
    Select("select * from 预定");

    ui->tableView->setModel(this->model);
    ui->radioButton->setText("预定车位");
    ui->radioButton_2->setText("预留电话");
    ui->radioButton->setVisible(true);
    ui->radioButton_2->setVisible(true);
}
void ShowWidget::SetStopModel()
{
    ui->radioButton->setVisible(false);
    ui->radioButton_2->setVisible(false);
    if(userName!="root")
    {
        ui->Insert_Button->setVisible(true);
        ui->Insert_Button->setText("停车");
        ui->Delete_Button->setVisible(false);
        ui->Edit_Button->setVisible(false);
    }
    Flush_RadioButton();
    this->model = new QStandardItemModel(ui->tableView);
    this->model->setColumnCount(6);
    this->model->setHeaderData(0,Qt::Horizontal, "停车ID");
    this->model->setHeaderData(1,Qt::Horizontal, "车位ID");
    this->model->setHeaderData(2,Qt::Horizontal, "规定开出时间");
    this->model->setHeaderData(3,Qt::Horizontal, "开入时间");
    this->model->setHeaderData(4, Qt::Horizontal, "车牌");
     this->model->setHeaderData(5, Qt::Horizontal, "电话");
    Select("select * from 停车");

    ui->tableView->setModel(this->model);
    ui->radioButton->setText("车位ID");
    ui->radioButton_2->setText("车牌");
    ui->radioButton->setVisible(true);
    ui->radioButton_2->setVisible(true);
}
void ShowWidget::SetConnect()
{
    connect(ui->Car_button,&QPushButton::clicked,[=](){
        this->setWindowTitle("车位表");
        this->SetCar_setModel();
    });
    connect(ui->Stop_button,&QPushButton::clicked,[=](){
        this->SetStopModel();
        this->setWindowTitle("停车表");

    });
    connect(ui->Out_button,&QPushButton::clicked,[=](){
        this->SetOutModel();
        this->setWindowTitle("开出表");
    });
    connect(ui->Pre_button,&QPushButton::clicked,[=](){
        this->SetPreModel();
        this->setWindowTitle("预定表");
    });
    connect(ui->User_button,&QPushButton::clicked,[=](){
        this->SetUserModel();
        this->setWindowTitle("用户表");
    });
    connect(ui->radioButton,&QRadioButton::toggled,ui->Search_line,[=]()
    {
        QString str = ui->radioButton->text();
        if(str=="车位号")
        {
             ui->Search_line->setPlaceholderText("1");
        }
        else if(str =="开出人员")
        {
             ui->Search_line->setPlaceholderText("张三");
        }
        else if(str=="用户名")
        {
             ui->Search_line->setPlaceholderText("张三");
        }
        else if(str == "预定车位")
        {
             ui->Search_line->setPlaceholderText("1");
        }
        else
        {
             ui->Search_line->setPlaceholderText("1");
        }
    });
    connect(ui->radioButton_2,&QRadioButton::toggled,ui->Search_line,[=]()
    {
        QString str = ui->radioButton_2->text();
        if(str=="预留电话")
        {
             ui->Search_line->setPlaceholderText("1532...");
        }
        else if(str == "是否有车")
        {
             ui->Search_line->setPlaceholderText("0");
        }
        else
        {
             ui->Search_line->setPlaceholderText("皖K...");
        }
    });
    connect(ui->Search_line,&QLineEdit::textChanged,ui->tableView,[=](const QString &text)
    {
        QString str = this->windowTitle();

        if(str=="停车表")
        {
            if(text=="")
            {
                Select("select * from 停车");
            }
            if(ui->radioButton->isChecked())
            {
                Select((QString("select * from 停车 where Set_id like '%1%'").arg(text)).toUtf8().data());
            }
            else if(ui->radioButton_2->isChecked())
            {
                Select((QString("select * from 停车 where Car_char like '%1%'").arg(text)).toUtf8().data());

            }
            else
            {
                QMessageBox::warning(this, tr("ERROR"),
                                     tr("未选中 "
                                        ""),
                                     QMessageBox::Yes
                                     | QMessageBox::Cancel);
            }

        }
        else if(str=="开出表")
        {
            if(text=="")
            {
                Select("select * from 开出");
            }
            if(ui->radioButton->isChecked())
            {
                Select((QString("select * from 开出 where Out_name like '%1%'").arg(text)).toUtf8().data());
            }
            else if(ui->radioButton_2->isChecked())
            {
                Select((QString("select * from 开出 where Car_char like '%1%'").arg(text)).toUtf8().data());

            }
            else
            {
                QMessageBox::warning(this, tr("ERROR"),
                                     tr("未选中 "
                                        ""),
                                     QMessageBox::Yes
                                     | QMessageBox::Cancel);
            }
        }
        else if(str=="用户表")
        {
            if(text=="")
            {
                Select("select * from 用户");
            }
            if(ui->radioButton->isChecked())
            {
                Select((QString("select * from 用户 where User_name like '%1%'").arg(text)).toUtf8().data());
            }
            else if(ui->radioButton_2->isChecked())
            {
                Select((QString("select * from 用户 where Car_char like '%1%'").arg(text)).toUtf8().data());

            }
            else
            {
                QMessageBox::warning(this, tr("ERROR"),
                                     tr("未选中 "
                                        ""),
                                     QMessageBox::Yes
                                     | QMessageBox::Cancel);
            }
        }
        else if(str=="预定表")
        {
            if(text=="")
            {
                Select("select * from 预定");
            }
            if(ui->radioButton->isChecked())
            {
                Select((QString("select * from 预定 where Set_id like '%1%'").arg(text)).toUtf8().data());
            }
            else if(ui->radioButton_2->isChecked())
            {
                Select((QString("select * from 预定 where Phone_name like '%1%'").arg(text)).toUtf8().data());

            }
            else
            {
                QMessageBox::warning(this, tr("ERROR"),
                                     tr("未选中 "
                                        ""),
                                     QMessageBox::Yes
                                     | QMessageBox::Cancel);
            }
        }
        else if(str=="车位表")
        {
            if(text=="")
                       {
                           Select("select * from 车位");
                       }
            if(ui->radioButton->isChecked())
                        {
                            Select((QString("select * from 车位 where Set_id like '%1%'").arg(text)).toUtf8().data());
                        }
                        else if(ui->radioButton_2->isChecked())
                        {
                            Select((QString("select * from 车位 where Have_car like '%1%'").arg(text)).toUtf8().data());

                        }
                        else
                        {
                            QMessageBox::warning(this, tr("ERROR"),
                                                 tr("未选中 "
                                                    ""),
                                                 QMessageBox::Yes
                                                 | QMessageBox::Cancel);
                        }
        }
        else
        {
            QMessageBox::warning(this, tr("ERROR"),
                                         tr("请选中其中的一个表进行查询 "
                                            ""),
                                         QMessageBox::Yes
                                         | QMessageBox::Cancel);
        }
    });
    connect(ui->Edit_Button,&QPushButton::clicked,this,[=](){
        if(this->windowTitle()=="车库管理系统")
        {
            QMessageBox::warning(this,tr("Waring"),tr("请选择一个表进行修改"));
        }
        else
        {
            Update();
        }
    });
    connect(ui->Insert_Button,&QPushButton::clicked,this,[=](){
        if(this->windowTitle()=="车库管理系统")
        {
            QMessageBox::warning(this,tr("Wring"),tr("请选择一个表进行插入"));
        }
        else
        {
            Insert();
        }
    });
    connect(ui->Delete_Button,&QPushButton::clicked,this,[=](){
        this->Delete();
    });

}
void ShowWidget::Flush_RadioButton()
{
    ui->radioButton->setAutoExclusive(false);
    ui->radioButton->setChecked(false);
    ui->radioButton->setAutoExclusive(true);
    ui->radioButton_2->setAutoExclusive(false);
    ui->radioButton_2->setChecked(false);
    ui->radioButton_2->setAutoExclusive(true);
    ui->Search_line->setPlaceholderText(" ");
}

